package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.PaymentApiOrder;
import com.jeequan.jeepay.service.PaymentApiOrderService;
import com.jeequan.jeepay.service.mapper.PaymentApiOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
//import .entity.PaymentApiOrder;
//import .mapper.PaymentApiOrderMapper;
//import .service.PaymentApiOrderService;
 /**
 * 代付订单;(payment_api_order)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PaymentApiOrderServiceImpl implements PaymentApiOrderService {
    @Autowired
    private PaymentApiOrderMapper paymentApiOrderMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public PaymentApiOrder queryById(Integer id){
        return paymentApiOrderMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param paymentApiOrder 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PaymentApiOrder> paginQuery(PaymentApiOrder paymentApiOrder, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PaymentApiOrder> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(paymentApiOrder.getOrderId())){
            queryWrapper.eq(PaymentApiOrder::getOrderId, paymentApiOrder.getOrderId());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getOutTradeNo())){
            queryWrapper.eq(PaymentApiOrder::getOutTradeNo, paymentApiOrder.getOutTradeNo());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBalanceSymbol())){
            queryWrapper.eq(PaymentApiOrder::getBalanceSymbol, paymentApiOrder.getBalanceSymbol());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getNotifyUrl())){
            queryWrapper.eq(PaymentApiOrder::getNotifyUrl, paymentApiOrder.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBankCardNo())){
            queryWrapper.eq(PaymentApiOrder::getBankCardNo, paymentApiOrder.getBankCardNo());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBankAccount())){
            queryWrapper.eq(PaymentApiOrder::getBankAccount, paymentApiOrder.getBankAccount());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBankCode())){
            queryWrapper.eq(PaymentApiOrder::getBankCode, paymentApiOrder.getBankCode());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getIfsc())){
            queryWrapper.eq(PaymentApiOrder::getIfsc, paymentApiOrder.getIfsc());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getMobile())){
            queryWrapper.eq(PaymentApiOrder::getMobile, paymentApiOrder.getMobile());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getEmail())){
            queryWrapper.eq(PaymentApiOrder::getEmail, paymentApiOrder.getEmail());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getExtend())){
            queryWrapper.eq(PaymentApiOrder::getExtend, paymentApiOrder.getExtend());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getRejectReason())){
            queryWrapper.eq(PaymentApiOrder::getRejectReason, paymentApiOrder.getRejectReason());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getRemark())){
            queryWrapper.eq(PaymentApiOrder::getRemark, paymentApiOrder.getRemark());
        }
        //2. 执行分页查询
        Page<PaymentApiOrder> pagin = new Page<>(current , size , true);
        IPage<PaymentApiOrder> selectResult = paymentApiOrderMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param paymentApiOrder 实例对象
     * @return 实例对象
     */
    public PaymentApiOrder insert(PaymentApiOrder paymentApiOrder){
        paymentApiOrderMapper.insert(paymentApiOrder);
        return paymentApiOrder;
    }
    
    /** 
     * 更新数据
     *
     * @param paymentApiOrder 实例对象
     * @return 实例对象
     */
    public PaymentApiOrder update(PaymentApiOrder paymentApiOrder){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PaymentApiOrder> chainWrapper = new LambdaUpdateChainWrapper<PaymentApiOrder>(paymentApiOrderMapper);
        if(StrUtil.isNotBlank(paymentApiOrder.getOrderId())){
            chainWrapper.set(PaymentApiOrder::getOrderId, paymentApiOrder.getOrderId());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getOutTradeNo())){
            chainWrapper.set(PaymentApiOrder::getOutTradeNo, paymentApiOrder.getOutTradeNo());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBalanceSymbol())){
            chainWrapper.set(PaymentApiOrder::getBalanceSymbol, paymentApiOrder.getBalanceSymbol());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getNotifyUrl())){
            chainWrapper.set(PaymentApiOrder::getNotifyUrl, paymentApiOrder.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBankCardNo())){
            chainWrapper.set(PaymentApiOrder::getBankCardNo, paymentApiOrder.getBankCardNo());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBankAccount())){
            chainWrapper.set(PaymentApiOrder::getBankAccount, paymentApiOrder.getBankAccount());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getBankCode())){
            chainWrapper.set(PaymentApiOrder::getBankCode, paymentApiOrder.getBankCode());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getIfsc())){
            chainWrapper.set(PaymentApiOrder::getIfsc, paymentApiOrder.getIfsc());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getMobile())){
            chainWrapper.set(PaymentApiOrder::getMobile, paymentApiOrder.getMobile());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getEmail())){
            chainWrapper.set(PaymentApiOrder::getEmail, paymentApiOrder.getEmail());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getExtend())){
            chainWrapper.set(PaymentApiOrder::getExtend, paymentApiOrder.getExtend());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getRejectReason())){
            chainWrapper.set(PaymentApiOrder::getRejectReason, paymentApiOrder.getRejectReason());
        }
        if(StrUtil.isNotBlank(paymentApiOrder.getRemark())){
            chainWrapper.set(PaymentApiOrder::getRemark, paymentApiOrder.getRemark());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PaymentApiOrder::getId, paymentApiOrder.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(paymentApiOrder.getId());
        }else{
            return paymentApiOrder;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = paymentApiOrderMapper.deleteById(id);
        return total > 0;
    }
}